﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using JumbotCms.Common;
namespace JumbotCms.Modules.Article.CollectItem
{
    public partial class _test : JumbotCms.API.AdminCenter
    {
        private string _err = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            Server.ScriptTimeout = 99999999;
            ChannelId = Str2Str(q("ccid"));
            id = Str2Str(q("id"));
            Admin_Load(ChannelId + "-01", "html", true);
            if (GetTest())
            {
                doh.Reset();
                doh.ConditionExpress = "id=" + id;
                doh.AddFieldItem("flag", 1);
                doh.Update("jcms_module_article_collitem");
            }
            else
            {
                doh.Reset();
                doh.ConditionExpress = "id=" + id;
                doh.AddFieldItem("flag", 0);
                doh.Update("jcms_module_article_collitem");
                FinalMessage(this._err, site.Dir + "admin/close.htm", 0);
            }
        }
        private bool GetTest()
        {
            if (id == "0")
            {
                this._err = "参数错误，项目ID不能为 0";
                return false;
            }
            doh.Reset();
            doh.SqlCmd = "SELECT * FROM [jcms_module_article_collitem] WHERE [Id]=" + id;
            DataTable dtCollItem = doh.GetDataTable();
            if (dtCollItem.Rows.Count == 0)
            {
                this._err = "ID为 " + id + " 的项目不存在!";
                return false;
            }
            int CollecNewsNum = Str2Int(dtCollItem.Rows[0]["CollecNewsNum"].ToString());
            string ListStr = dtCollItem.Rows[0]["ListStr"].ToString();
            string LsString = dtCollItem.Rows[0]["LsString"].ToString();
            string LoString = dtCollItem.Rows[0]["LoString"].ToString();
            string HsString = dtCollItem.Rows[0]["HsString"].ToString();
            string HoString = dtCollItem.Rows[0]["HoString"].ToString();
            string ListWebEncode = dtCollItem.Rows[0]["ListWebEncode"].ToString();
            string ContentWebEncode = dtCollItem.Rows[0]["ContentWebEncode"].ToString();
            string TsString = dtCollItem.Rows[0]["TsString"].ToString();
            string ToString = dtCollItem.Rows[0]["ToString"].ToString();
            string CsString = dtCollItem.Rows[0]["CsString"].ToString();
            string CoString = dtCollItem.Rows[0]["CoString"].ToString();
            string NPsString = dtCollItem.Rows[0]["NPsString"].ToString();
            string NPoString = dtCollItem.Rows[0]["NPoString"].ToString();
            string SaveFiles = dtCollItem.Rows[0]["SaveFiles"].ToString();
            string CollecOrder = dtCollItem.Rows[0]["CollecOrder"].ToString();
            dtCollItem.Clear();
            dtCollItem.Dispose();
            System.Text.Encoding LencodeType = System.Text.Encoding.Default;
            System.Text.Encoding CencodeType = System.Text.Encoding.Default;
            switch (ListWebEncode)
            {
                case "3":
                    LencodeType = System.Text.Encoding.Unicode;
                    break;
                case "2":
                    LencodeType = System.Text.Encoding.UTF8;
                    break;
                case "1":
                    LencodeType = System.Text.Encoding.GetEncoding("GB2312");
                    break;
            }
            switch (ContentWebEncode)
            {
                case "3":
                    CencodeType = System.Text.Encoding.Unicode;
                    break;
                case "2":
                    CencodeType = System.Text.Encoding.UTF8;
                    break;
                case "1":
                    CencodeType = System.Text.Encoding.GetEncoding("GB2312");
                    break;
            }
            NewsCollection nc = new NewsCollection();
            string testList = nc.GetHttpPage(ListStr, 10000, LencodeType);
            if (testList == "$UrlIsFalse$")
            {
                this._err = "列表地址设置错误";
                return false;
            }
            if (testList == "$GetFalse$")
            {
                this._err = "无法连接列表页或连接超时";
                return false;
            }
            testList = nc.GetBody(testList, LsString, LoString, false, false);
            if (testList == "$StartFalse$")
            {
                this._err = "列表开始前标记设置错误,请重新设置";
                return false;
            }
            if (testList == "$EndFalse$")
            {
                this._err = "列表结束后标记设置错误,请重新设置";
                return false;
            }
            System.Collections.ArrayList linkArray = nc.GetArray(testList, HsString, HoString);
            if (linkArray.Count == 0)
            {
                this._err = "未取到链接,请检查链接设置";
                return false;
            }
            if (linkArray[0].ToString() == "$StartFalse$")
            {
                this._err = "链接开始前标记设置错误,请重新设置";
                return false;
            }
            if (linkArray[0].ToString() == "$EndFalse$")
            {
                this._err = "链接结束后标记设置错误,请重新设置";
                return false;
            }
            if (linkArray[0].ToString() == "$NoneLink$")
            {
                this._err = "未取到链接,请检查链接设置";
                return false;
            }
            if (CollecOrder == "1")
                linkArray.Reverse();
            if (CollecNewsNum > 0 && linkArray.Count > CollecNewsNum)
                linkArray.RemoveRange(CollecNewsNum, linkArray.Count - CollecNewsNum);

            string linkStr = string.Empty;
            linkStr = nc.DefiniteUrl(linkArray[0].ToString(), ListStr);
            if (linkStr == "$False$")
            {
                this._err = "获取到的链接地址无效,请检查链接设置";
                return false;
            }
            string newsCode = nc.GetHttpPage(linkStr, 10000, CencodeType);
            if (newsCode == "$UrlIsFalse$")
            {
                this._err = "获取到的链接地址无效,请检查链接设置";
                return false;
            }
            if (newsCode == "$GetFalse$")
            {
                this._err = "无法连接内容页或连接超时";
                return false;
            }
            string cTitle = nc.GetBody(newsCode, TsString, ToString, false, false);
            string cBody = nc.GetBody(newsCode, CsString, CoString, false, false);
            if (cTitle == "$StartFalse$" || cBody == "$StartFalse$")
            {
                this._err = "标题或正文开始前标记设置错误,请重新设置";
                return false;
            }
            if (cTitle == "$EndFalse$" || cBody == "$EndFalse$")
            {
                this._err = "标题或正文结束后标记设置错误,请重新设置";
                return false;
            }
            ltTestTitle.Text = cTitle;
            System.Collections.ArrayList bodyArray = nc.ReplaceSaveRemoteFile(cBody, Request.PhysicalApplicationPath + "\\" + ChannelDir, site.Dir + ChannelDir, linkStr, "0");
            ltTestContent.Text = bodyArray[0].ToString();
            if (bodyArray.Count == 3)
            {
                ltPhotoUrl.Text = bodyArray[2].ToString();
            }
            return true;
        }
    }
}
